package com.lsy.seckill.controller;

import com.lsy.seckill.pojo.User;
import com.lsy.seckill.service.GoodsService;
import com.lsy.seckill.vo.SeckillGoodsVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    //手动渲染需要的模板解析器
    @Resource
    private ThymeleafViewResolver thymeleafViewResolver;
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(User user, Model model,
                         HttpServletRequest request,HttpServletResponse response) {
        if (user == null) {
            WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
            return thymeleafViewResolver.getTemplateEngine().process("login",webContext);
        }
        //先到redis获取页面-如果有,直接返回页面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsList");
        if (StringUtils.hasText(html)) {
            return html;
        }
        model.addAttribute("user", user);
        List<SeckillGoodsVo> goodsList = goodsService.findSeckillGoodsVo();
        model.addAttribute("goodsList", goodsList);
        WebContext webContext = new WebContext(request, response,
                request.getServletContext(), request.getLocale(), model.asMap());
         html = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
        if (StringUtils.hasText(html)) {
            //将页面保存到redis, 设置每60s更新一次, 该页面60s失效,Redis会清除该页面
            valueOperations.set("goodsList", html, 60, TimeUnit.SECONDS);
        }
        return html;
    }

    @RequestMapping(value = "/toDetail/{goodsId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail(User user, @PathVariable Long goodsId, Model model,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        if (user == null) {
            WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
            return thymeleafViewResolver.getTemplateEngine().process("login",webContext);
        }
        //先到redis获取页面-如果有,直接返回页面
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsDetail:" + goodsId);
        if (StringUtils.hasText(html)) {
            return html;
        }
        SeckillGoodsVo seckillGoodsVo = goodsService.findSeckillGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods", seckillGoodsVo);
        model.addAttribute("user", user);
//        secKillStatus 0未开始 1进行中 2已结束
//        remainSeconds 大于0 还有remainSeconds秒开始 0进行中 -1已结束
        int secKillStatus = 0;
        int remainSeconds = 0;
        Date startDate = seckillGoodsVo.getStartDate();
        Date endDate = seckillGoodsVo.getEndDate();
        Date nowDate = new Date();
        if (nowDate.before(startDate)) {
            //秒杀还未开始
            secKillStatus = 0;
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        } else if (nowDate.after(endDate)) {
            //秒杀已结束
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀进行中
            secKillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
        if (StringUtils.hasText(html)) {
            //将页面保存到redis, 设置每60s更新一次, 该页面60s失效,Redis会清除该页面
            valueOperations.set("goodsDetail:" + goodsId, html, 60, TimeUnit.SECONDS);
        }
        return html;
    }
}
